package sk.uniba.fmph.asdf.db;

import java.util.regex.Pattern;

import sk.uniba.fmph.asdf.SerializableEntity;

/**
 * Podtrieda serializovatelnej entity sluzi k uchovaniu udajov o prihlasenom uzivatelovi.
 * @serial
 *
 * @author Marek Timura
 */
public class Account extends SerializableEntity {
	private static final long serialVersionUID = 3407272617325224005L;

	/** Regexovy vzor, ktory vyjadruje validny format emailovej adresy resp. prihlasovacieho mena. */
    public static final Pattern validEmail = Pattern.compile("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+"
            + "(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$");

    /**
     * id uzivatela
     * @serialField
     */
    private int id;

    /**
     * email resp prihlasovacie meno
     * @serialField
     */
    private String email;

    /**
     * cele meno
     * @serialField
     */
    private FullName fullName;

    /**
     * true ak ma prava administratora, inak false
     * @serialField
     */
    private boolean admin;

    /**
     * Metoda overi stav nacitaneho objektu.
     */
    @Override
    public void validateState() {
        validateId(id);
        validateEmail(email);
        fullName.validateState();
    }

    /**
     * Metoda overi spravny format emailovej adresy resp. prihlasovacieho mena.
     * @param email emailova adresa resp. prihlasovacie meno uzivatela
     */
    private void validateEmail(final String email) {
        if (!validEmail.matcher(email).matches()) {
            throw new IllegalArgumentException(email);
        }
    }

    public Account() {

    }

    /**
     * @param id id uzivatela
     * @param email email resp. prihlasovacie meno
     * @param fullName cele meno
     */
    public Account(final int id, final String email, final FullName fullName) {
        this.id = id;
        this.email = email;
        this.fullName = fullName;
        this.admin = (id == 0);
    }

    /**
     * Vracia cele meno uzivatela.
     * @return krstne meno a priezvisko oddelene medzerou
     */
    @Override
	public String toString() {
		return fullName.getFirstName() + " " + fullName.getLastName();
	}

	/**
     * Vracia id uzivatela.
     * @return id uzivatela
     */
    public int getId() {
        return id;
    }

    /**
     * Vrati email / prihlasovacie meno uzivatela.
     * @return email / prihlasovacie meno uzivatela
     */
    public String getEmail() {
        return email;
    }

    /**
     * Vrati cele meno uzivatela ak objekt
     * @return objekt, ktory uchovava cele meno uzivatela
     */
    public FullName getFullName() {
        return fullName;
    }

    /**
     * Vrati true ak uzivatel je administrator, v opacnom pripade false.
     * @return boolovsku hodnotu, vypovedajucu o administratorskych opravneniach uzivatela
     */
    public boolean isAdmin() {
        return admin;
    }

    /**
	 * Porovna objekty podla ID uctu.
	 *
	 * @param obj
	 *            Objekt, s ktorym sa ma ucet porovnat.
	 * @return True, ak sa ID uctov zhoduje, inac false. Ak porovnavany objekt
	 *         nie je triedy Account, vrati false.
	 */
    @Override
    public boolean equals(Object obj) {
    	if (!(obj instanceof Account)) {
    		return false;
    	}
    	Account acc = (Account) obj;
    	return id == acc.getId();
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 37 * hash + this.id;
        return hash;
    }
}
